import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
import { FormBuilder, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
import { I18nService } from 'src/app/core/i18n/i18n.service';

@Component({
    templateUrl: './index.html',
    styleUrls: ['./index.scss']
})
export class AddRechargeComponent implements OnInit {
	validateForm;
	datalist = [];
	isVisible = false;
	userLevel = {};
    constructor(
        private apiService: ApiService,
        private message: MessageService,
        private router: Router,
        private fb: FormBuilder,
		private nzmessage: NzMessageService,
		private i18n:I18nService
    ) {
		this.i18n.get('userLevel').subscribe(res=>{
			this.userLevel = res;
		});
    }
    ngOnInit() {
		this.message.setBreadcrumb({
            breadcrumbs: [
                { name: '充值管理' }
            ]
        });
        this.validateForm = this.fb.group({
			userId: [null, [Validators.required]],
			amount: [null, [Validators.required,Validators.pattern('(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)')]],
			bonusAmount: [null, [Validators.required,Validators.pattern('(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)')]],
		});
    }
    search(e){
		this.apiService.post('会员详情',{loginId:e},null,true).then(res=>{
			if (res['data'] && res['data'].userDetailForMaster) {
				this.datalist = [res['data'].userDetailForMaster];
			}
		}).catch(res=>{

		})
	}
	change(e){
		if(!e){
			this.datalist = [];
		}
	}
	userDetail(){
		if(this.validateForm.get('userId').value){
			this.isVisible = true;
		}
	}
	handleCancel(){
		this.isVisible = false;
	}
	submit(){
		for (const v in this.validateForm.controls) {
			this.validateForm.controls[v].markAsDirty();
			this.validateForm.controls[v].updateValueAndValidity();
		}
		if (this.validateForm.valid) {
			this.apiService.post('创建充值记录',this.validateForm.value).then(res=>{
				this.nzmessage.success('充值记录创建成功！');
				this.router.navigateByUrl('/basicSetting/recharge');
			}).catch(res=>{
	
			})
		}
	}
}
